package com.sunshine.wrongquestionset.room.dao

import androidx.lifecycle.LiveData
import androidx.room.*

import com.sunshine.wrongquestionset.room.entity.QuestionEntity

/**
 * @author sunshine
 * @date 2021/3/26
 * @see com.sunshine.wrongquestionset.room.entity.QuestionEntity
 *
 * 2021 04 10更新 删除了有关科目和熟悉度的操作
 */
@Dao
interface QuestionDao {
    //插入题目情况
    @Query("INSERT INTO question(userId, questionSetId, subjectId, typeId, tag, titlePath, answerPath, familiarity, difficulty, lastTime, doCount, note, insertTime, score) VALUES(:userId,:questionSetId,:subjectId,:typeId,:tag,:titlePath,:answerPath,:familiarity,:difficulty,:lastTime,:doCount,:note,:insertTime,:score)")
    fun insert(
        userId: String,
        questionSetId: Int,
        subjectId: Int,
        typeId: Int,
        tag: String,
        titlePath: String,
        answerPath: String,
        familiarity: Float,
        difficulty: Float,
        lastTime: Long,
        doCount: Int,
        note: String?,
        insertTime: Long,
        score: Float
    )

    //通过实体类删除一个题目
    @Delete(entity = QuestionEntity::class)
    fun deleteByEntity(questionEntity: QuestionEntity)

//    //删除熟悉程度在某一数值以上的题目，包括这个熟悉值
//    @Query("DELETE FROM question WHERE userId=:userId and familiarity>=:familiarity")
//    fun deleteByFamiliarity(userId: String, familiarity: Float)
//
//    //删除某一错题集中熟悉程度在某一数值以上的题目，包括这个熟悉值
//    @Query("DELETE FROM question WHERE questionSetId=:questionSetId and familiarity>=:familiarity")
//    fun deleteByFamiliarityAndSetId(questionSetId: Int, familiarity: Float)

    //删除某用户所有题目
    @Query("DELETE FROM question WHERE userId=:userId")
    fun deleteAll(userId: String)

    //删除某错题集所有题目
    @Query("DELETE FROM question WHERE questionSetId=:questionSetId")
    fun deleteAllInSetId(questionSetId: Int)

    //查询所有做题情况
    @Query("SELECT * FROM question")
    fun selectAll(): LiveData<List<QuestionEntity>?>

    @Query("SELECT * FROM question WHERE id=:id")
    fun selectById(id: Int): LiveData<QuestionEntity?>

    //查询某错题集所有做题情况
    @Query("SELECT * FROM question WHERE questionSetId=:questionSetId")
    fun selectAllInSetId(questionSetId: Int): LiveData<List<QuestionEntity>?>

    //查询某用户所有做题情况
    @Query("SELECT * FROM question WHERE userId=:userId")
    fun selectUserAll(userId: String): LiveData<List<QuestionEntity>?>

    //查询指定科目分类id的题目
    @Query("SELECT * FROM question WHERE subjectId=:subjectId")
    fun selectBySubject(subjectId: Int): LiveData<List<QuestionEntity>?>
//
//    //查询某错题集中指定科目分类id的题目
//    @Query("SELECT * FROM question WHERE questionSetId=:questionSetId and subjectId=:subjectId")
//    fun selectBySubjectAndSetId(questionSetId: Int, subjectId: Int): LiveData<List<QuestionEntity>?>

    //查询指定类型分类id的题目
    @Query("SELECT * FROM question WHERE userId=:userId and typeId=:typeId")
    fun selectByType(userId: String, typeId: Int): LiveData<List<QuestionEntity>?>

    //查询某错题集指定类型分类id的题目
    @Query("SELECT * FROM question WHERE questionSetId=:questionSetId and typeId=:typeId")
    fun selectByTypeAndSetId(questionSetId: Int, typeId: Int): LiveData<List<QuestionEntity>?>

//    //查询熟悉度大于指定熟悉度的题目
//    @Query("SELECT * FROM question WHERE userId=:userId and familiarity>:familiarity")
//    fun selectByGreaterFamiliarity(userId: String, familiarity: Float): LiveData<List<QuestionEntity>?>
//
//    //查询某错题集熟悉度大于指定熟悉度的题目
//    @Query("SELECT * FROM question WHERE questionSetId=:questionSetId and familiarity>:familiarity")
//    fun selectByGreaterFamiliarityAndSetId(questionSetId: Int, familiarity: Float): LiveData<List<QuestionEntity>?>
//
//    //查询熟悉度小于指定熟悉度的题目
//    @Query("SELECT * FROM question WHERE userId=:userId and familiarity<:familiarity")
//    fun selectByLessFamiliarity(userId: String, familiarity: Float): LiveData<List<QuestionEntity>?>
//
//    //查询某错题集熟悉度小于指定熟悉度的题目
//    @Query("SELECT * FROM question WHERE questionSetId=:questionSetId and familiarity<:familiarity")
//    fun selectByLessFamiliarityAndSetId(questionSetId: Int, familiarity: Float): LiveData<List<QuestionEntity>?>

    //查询熟悉度大于指定难度的题目
    @Query("SELECT * FROM question WHERE userId=:userId and difficulty>:difficulty")
    fun selectByGreaterDifficulty(userId: String, difficulty: Float): LiveData<List<QuestionEntity>?>

    //查询某错题集熟悉度大于指定难度的题目
    @Query("SELECT * FROM question WHERE questionSetId=:questionSetId and difficulty>:difficulty")
    fun selectByGreaterDifficultyAndSetId(questionSetId: Int, difficulty: Float): LiveData<List<QuestionEntity>?>

    //查询熟悉度小于指定难度的题目
    @Query("SELECT * FROM question WHERE userId=:userId and difficulty<:difficulty")
    fun selectByLessDifficulty(userId: String, difficulty: Float): LiveData<List<QuestionEntity>?>

    //查询最新插入的问题
    @Query("SELECT * FROM question ORDER BY id DESC LIMIT 1")
    fun selectLastQuestion(): LiveData<QuestionEntity>

    //查询熟悉度小于指定难度的题目
    @Query("SELECT * FROM question WHERE questionSetId=:questionSetId and difficulty<:difficulty")
    fun selectByLessDifficultyAndSetId(questionSetId: Int, difficulty: Float): LiveData<List<QuestionEntity>?>

    @Query("SELECT COUNT(*) FROM question WHERE questionSetId=:setId")
    fun selectQuestionCountInSetId(setId: Int): LiveData<Int>

    //根据实体类更新一个题目
    @Update(entity = QuestionEntity::class, onConflict = OnConflictStrategy.REPLACE)
    fun updateByEntity(questionEntity: QuestionEntity)

    //更新熟悉程度
    @Query("UPDATE question SET familiarity=:familiarity WHERE id=:id")
    fun updateFamiliarity(id: Int, familiarity: Float)
}